package wikitools.model;

import java.util.Date;
import java.util.List;

import wikitools.util.Default;

import com.db4o.Db4o;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Predicate;

public class Article implements Idable{
	public RSSDate getPubDate() {
        return pubDate;
    }

    public void setPubDate(RSSDate pubDate) {
        this.pubDate = pubDate;
    }

    private String id;
	private String title;
	private String author;
	private String content;
	private RSSDate created;
	private RSSDate pubDate;
	


	public Article(String id, String title, String author, String content) {
		super();
		this.author = author;
		this.content = content;
		this.id = id;
		this.title = title;
		this.pubDate = new RSSDate();
	}
	
   public Article(String title, String author, String content) {
        this("", title, author, content);
    }
	
	public Article clone(){
		Article a = new Article(this.id, this.title, this.author, this.content);
		return a;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String toString() {
		return this.getClass().getSimpleName() + ":["
		    + id + ", " + title + ", " + author + ", " 
		    + pubDate.toString() + "]" + this.hashCode();
	}

	public static void listResult(ObjectSet<?> result) {
		System.out.println(result.size());
		while (result.hasNext()) {
			System.out.println(result.next());
		}
	}

	public static void main(String[] args) {
		// new File(Default.DBFILE).delete();
		ObjectContainer db = Db4o.openFile(Default.DBFILE);
		try {
			Article a1 = new Article("haha", "My Fist Article", "zhaop",
					"Silence! I kill You!");
			DBUtil.storeWithCheck(a1, db);
			System.out.println("object stored: " + a1);


			List<Article> list = db.query(new Predicate<Article>() {
				
				private static final long serialVersionUID = 1L;

				public boolean match(Article candidate) {
					return candidate.getAuthor().equals("zhaop");
				}
			});

			System.out.println(list.size());
			for (Article i : list) {
				System.out.println(i);
			}

		} finally {
			db.close();
		}

	}
}
